import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# 创建虚拟数据
np.random.seed(0)
provinces = ['四川', '云南', '贵州', '西藏', '陕西', '甘肃', '青海', '宁夏', '新疆', '重庆']
years = np.arange(2005, 2021)
data = {
    'Province': np.repeat(provinces, len(years)),
    'Year': np.tile(years, len(provinces)),
    'Water': np.random.rand(len(provinces) * len(years)) * 0.5 + 0.1,
    'Energy': np.random.rand(len(provinces) * len(years)) * 0.5 + 0.1,
    'Food': np.random.rand(len(provinces) * len(years)) * 0.5 + 0.1,
    'Composite_Index': np.random.rand(len(provinces) * len(years)) * 0.5 + 0.1
}

# 转换为DataFrame
df = pd.DataFrame(data)

# 数据标准化
df[['Water', 'Energy', 'Food', 'Composite_Index']] = (df[['Water', 'Energy', 'Food', 'Composite_Index']] - df[['Water', 'Energy', 'Food', 'Composite_Index']].min()) / (df[['Water', 'Energy', 'Food', 'Composite_Index']].max() - df[['Water', 'Energy', 'Food', 'Composite_Index']].min())

# 创建省份编码
province_codes = {province: i for i, province in enumerate(provinces)}
df['Province_Code'] = df['Province'].map(province_codes)

# 创建图表
fig = plt.figure(figsize=(12, 10))

# 水资源子图
ax1 = fig.add_subplot(221, projection='3d')
ax1.scatter(df['Year'], df['Province_Code'], df['Water'], c='b', marker='o')
ax1.set_title('Water Resource Development Index')
ax1.set_xlabel('Year')
ax1.set_ylabel('Province Code')
ax1.set_zlabel('Index')
ax1.view_init(elev=20, azim=120)

# 能源子图
ax2 = fig.add_subplot(222, projection='3d')
ax2.scatter(df['Year'], df['Province_Code'], df['Energy'], c='r', marker='o')
ax2.set_title('Energy System Development Index')
ax2.set_xlabel('Year')
ax2.set_ylabel('Province Code')
ax2.set_zlabel('Index')
ax2.view_init(elev=20, azim=120)

# 粮食子图
ax3 = fig.add_subplot(223, projection='3d')
ax3.scatter(df['Year'], df['Province_Code'], df['Food'], c='g', marker='o')
ax3.set_title('Food System Development Index')
ax3.set_xlabel('Year')
ax3.set_ylabel('Province Code')
ax3.set_zlabel('Index')
ax3.view_init(elev=20, azim=120)

# 复合系统子图
ax4 = fig.add_subplot(224, projection='3d')
ax4.scatter(df['Year'], df['Province_Code'], df['Composite_Index'], c='k', marker='o')
ax4.set_title('Composite System Development Index')
ax4.set_xlabel('Year')
ax4.set_ylabel('Province Code')
ax4.set_zlabel('Index')
ax4.view_init(elev=20, azim=120)

plt.tight_layout()
plt.show()